﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CMS.IRepository;


namespace CMS.Repository
{
    public class DepartmentRepository :IDepartmentRepository
    {
        private CMSEntities _connectObject;

        /// <summary>
        /// Initializes a new instance of the <see cref="DepartmentRepository"/> class.
        /// </summary>
        /// <param name="connectionInstance">The connection instance.</param>
        public DepartmentRepository(CMSEntities connectionInstance)
        {
            _connectObject = connectionInstance;

        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DepartmentRepository"/> class.
        /// </summary>
        public DepartmentRepository()
        {
            _connectObject = new CMSEntities();

        }

        /// <summary>
        /// Selects the department by id.
        /// </summary>
        /// <param name="departmentId">The department id.</param>
        /// <returns></returns>
        public List<CMS.Model.Department> SelectDepartmentById(int departmentId)
        {

            var result = from d in _connectObject.Departments
                         where d.DepartmentId == departmentId
                         select d;


            return Translator.DepartmentTranslator.ToDepartmentModelList(result.ToList());

        }

        /// <summary>
        /// Selects all department.
        /// </summary>
        /// <param name="pageIndex">Index of the page.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <returns></returns>
        public List<CMS.Model.Department> SelectAllDepartment(int pageIndex, int pageSize)
        {

            var result = (from d in _connectObject.Departments
                         select d).OrderBy(d=>d.DepartmentId).Skip(pageIndex*pageSize).Take(pageSize);

            return Translator.DepartmentTranslator.ToDepartmentModelList(result.ToList());
        }


        /// <summary>
        /// Selects all department.
        /// </summary>
        /// <returns></returns>
        public List<CMS.Model.Department> SelectAllDepartment()
        {

            var result = from d in _connectObject.Departments
                          select d;

            return Translator.DepartmentTranslator.ToDepartmentModelList(result.ToList());
        }
        /// <summary>
        /// Updates the department.
        /// </summary>
        /// <param name="departmentInstance">The department instance.</param>
        public void UpdateDepartment(CMS.Model.Department departmentInstance)
        {
            var result = from d in _connectObject.Departments
                         where d.DepartmentId == departmentInstance.DepartmentId
                         select d;
            foreach (var p in result)
            {
                p.DepartmentName = departmentInstance.DepartmentName;
                p.Manager = departmentInstance.Manager;
                p.Phone = departmentInstance.Phone;
                p.Remark = departmentInstance.Remark;
                _connectObject.SaveChanges();
            }
        }

        /// <summary>
        /// Inserts the department.
        /// </summary>
        /// <param name="departmentInstance">The department instance.</param>
        public void InsertDepartment(CMS.Model.Department departmentInstance)
        { }

        public void DeleteDepartmentById(int departmentId)
        { }
    }
}
